package hust.music.action;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import hust.music.action.base.ActionBase;
import hust.music.entity.User;
import hust.music.service.HomePageModService;
import hust.music.util.DynamicInfo;
import hust.music.util.Page;

/**
 * FollowDynamicAction
 * 
 * @author Txazo
 * @since 1.0
 */
@Controller
@Scope("session")
public class FollowDynamicAction extends ActionBase {

	private static final long serialVersionUID = 1L;

	/** 当前页 */
	private int pageNow;
	/** 每页显示数 */
	private int pageSize = 10;
	/** 分页 */
	private Page page;

	/** 用户关注的人动态信息 */
	private List<DynamicInfo> dynamicInfoMap = new ArrayList<DynamicInfo>();
	/** 我最近上传的音乐 */
	private List<DynamicInfo> recentUploadMusic = new ArrayList<DynamicInfo>();
	/** 我的流派集 */
	private List<DynamicInfo> playList = new ArrayList<DynamicInfo>();
	/** 我的好友粉丝榜 */
	private List<DynamicInfo> fanRank = new ArrayList<DynamicInfo>();
	/** 我的音乐收听榜 */
	private List<DynamicInfo> musicListeningRank = new ArrayList<DynamicInfo>();
	/** 我的留言板 */
	private List<DynamicInfo> message = new ArrayList<DynamicInfo>();

	@Autowired
	private HomePageModService homePageModService;

	/**
	 * 获取用户关注的人动态信息
	 * 
	 * @return 逻辑视图
	 */
	public String next() {
		User user = (User) session.get("user");
		User home = (User) session.get("home");

		int first = (pageNow - 1) * pageSize;
		int length = 0;

		if ( user!=null && (user.getId() == home.getId())) {
			length = homePageModService.getTotalFollowDynamicInfo(home);
			dynamicInfoMap = homePageModService.getFollowDynamicInfo(home,
					first, this.getPageSize());
		} else {
			length = homePageModService.getTotalUserDynamicInfo(home);
			dynamicInfoMap = homePageModService.getUserDynamicInfo(home, first,
					this.getPageSize());
		}

		page = new Page(this.getPageNow(), this.getPageSize(), length);

		return "next";
	}

	public String musics(){
		User user = (User) session.get("user");
		
		int first = (pageNow - 1) * pageSize;
		int length = homePageModService.getTotalMusicsInfo(user);
		recentUploadMusic = homePageModService.getRecentUploadMusic(user, first, 10);
		page = new Page(this.getPageNow(), this.getPageSize(), length);
		return "musics";
	}
	/**
	 * 获取我的动态
	 * 
	 * @return 逻辑视图
	 * @throws Exception
	 */
	public String info() throws Exception {
		User user = (User) session.get("home");

		recentUploadMusic = homePageModService.getRecentUploadMusic(user, 0, 10);
		playList = homePageModService.getPlayList(user);
		fanRank = homePageModService.getFanRank(user, 0, 3);
		musicListeningRank = homePageModService.getMusicListeningRank(user, 0,
				5);

		return "info";
	}

	
	/**
	 * 获取用户留言
	 * 
	 * @return 逻辑视图
	 * @throws Exception
	 */
	public String message() throws Exception {
		User user = (User) session.get("home");

		int first = (pageNow - 1) * pageSize;
		int length = homePageModService.getTotalMessage(user);

		message = homePageModService
				.getMessage(user, first, this.getPageSize());

		page = new Page(this.getPageNow(), this.getPageSize(), length);

		return "message";
	}

	public List<DynamicInfo> getDynamicInfoMap() {
		return dynamicInfoMap;
	}

	public void setDynamicInfoMap(List<DynamicInfo> dynamicInfoMap) {
		this.dynamicInfoMap = dynamicInfoMap;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<DynamicInfo> getMessage() {
		return message;
	}

	public void setMessage(List<DynamicInfo> message) {
		this.message = message;
	}

	public List<DynamicInfo> getRecentUploadMusic() {
		return recentUploadMusic;
	}

	public void setRecentUploadMusic(List<DynamicInfo> recentUploadMusic) {
		this.recentUploadMusic = recentUploadMusic;
	}

	public List<DynamicInfo> getPlayList() {
		return playList;
	}

	public void setPlayList(List<DynamicInfo> playList) {
		this.playList = playList;
	}

	public List<DynamicInfo> getFanRank() {
		return fanRank;
	}

	public void setFanRank(List<DynamicInfo> fanRank) {
		this.fanRank = fanRank;
	}

	public List<DynamicInfo> getMusicListeningRank() {
		return musicListeningRank;
	}

	public void setMusicListeningRank(List<DynamicInfo> musicListeningRank) {
		this.musicListeningRank = musicListeningRank;
	}

}
